package com.anjubao.parking.municipal.common.config;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import org.springframework.cloud.openfeign.FeignFormatterRegistrar;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

import com.anjubao.parking.municipal.common.utils.U;

@Configuration
public class DateTimeConfig {

	/** 默认日期时间格式 */
	public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	/** 默认日期格式 */
	public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
	/** 默认时间格式 */
	public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

	/**
	 * LocalDate转换器，用于转换RequestParam和PathVariable参数
	 */
	@Bean
	public Converter<String, LocalDate> localDateConverter() {
		return new Converter<String, LocalDate>() {
			@Override
			public LocalDate convert(String source) {
				if (U.isEmpty(source)) {
					return null;
				}
				return LocalDate.parse(source, DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT));
			}
		};
	}

	/**
	 * LocalDateTime转换器，用于转换RequestParam和PathVariable参数
	 */
	@Bean
	public Converter<String, LocalDateTime> localDateTimeConverter() {
		return new Converter<String, LocalDateTime>() {
			@Override
			public LocalDateTime convert(String source) {
				if (U.isEmpty(source)) {
					return null;
				}
				return LocalDateTime.parse(source, DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT));
			}
		};
	}

	/**
	 * LocalTime转换器，用于转换RequestParam和PathVariable参数
	 */
	@Bean
	public Converter<String, LocalTime> localTimeConverter() {
		return new Converter<String, LocalTime>() {
			@Override
			public LocalTime convert(String source) {
				if (U.isEmpty(source)) {
					return null;
				}
				return LocalTime.parse(source, DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT));
			}
		};
	}

	/**
	 * Date转换器，用于转换RequestParam和PathVariable参数
	 */
	@Bean
	public Converter<String, Date> dateConverter() {
		return new Converter<String, Date>() {
			@Override
			public Date convert(String source) {
				if (U.isEmpty(source)) {
					return null;
				}
				SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
				try {
					return format.parse(source);
				} catch (ParseException e) {
					throw new RuntimeException(e);
				}
			}
		};
	}

	/**
	 * LocalDate转换器，用于转换RequestParam和PathVariable参数
	 */
	@Bean
	public Converter<LocalDate, String> localDateStrConverter() {
		return new Converter<LocalDate, String>() {
			@Override
			public String convert(LocalDate source) {
				if (U.isEmpty(source)) {
					return null;
				}

				return source.format(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT));
			}
		};
	}

	/**
	 * LocalDateTime转换器，用于转换RequestParam和PathVariable参数
	 */
	@Bean
	public Converter<LocalDateTime, String> localDateTimeStrConverter() {
		return new Converter<LocalDateTime, String>() {
			@Override
			public String convert(LocalDateTime source) {
				if (U.isEmpty(source)) {
					return null;
				}
				return source.format(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT));
			}
		};
	}

	/**
	 * LocalTime转换器，用于转换RequestParam和PathVariable参数
	 */
	@Bean
	public Converter<LocalTime, String> localTimeStrConverter() {
		return new Converter<LocalTime, String>() {
			@Override
			public String convert(LocalTime source) {
				if (U.isEmpty(source)) {
					return null;
				}
				return source.format(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT));
			}
		};
	}

	/**
	 * Date转换器，用于转换RequestParam和PathVariable参数
	 */
	@Bean
	public Converter<Date, String> dateStrConverter() {
		return new Converter<Date, String>() {
			@Override
			public String convert(Date source) {
				if (U.isEmpty(source)) {
					return null;
				}
				SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
				return format.format(source);
			}
		};
	}

	@Bean
	public FeignFormatterRegistrar localDateTimeFormatRegister() {
		return registry -> registry.addConverter(localDateTimeStrConverter());
	}

	@Bean
	public FeignFormatterRegistrar localDateFormatRegister() {
		return registry -> registry.addConverter(localDateStrConverter());
	}

	@Bean
	public FeignFormatterRegistrar localTimeFormatRegister() {
		return registry -> registry.addConverter(localTimeStrConverter());
	}

	@Bean
	public FeignFormatterRegistrar localDataFormatRegister() {
		return registry -> registry.addConverter(dateStrConverter());
	}
}
